package com.privatework.backedserver.controller.user;

import com.privatework.common.constants.SystemConstant;
import com.privatework.common.repository.user.UserRepository;
import com.privatework.common.requestvo.system.UserReqVo;
import com.privatework.common.service.system.UserService;
import com.privatework.common.utils.PageUtil;
import com.privatework.common.utils.ResultVoUtil;
import com.privatework.common.vo.PageVo;
import com.privatework.common.vo.ResultVo;
import com.privatework.common.vo.system.UserResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * 用户 Controller
 *
 * @author johnny
 * @create 2020-07-10 下午4:08
 **/
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/logout")
    public ResultVo userInfo() {
        return ResultVoUtil.success();
    }

    @GetMapping("/userInfo")
    public ResultVo userInfo(@RequestParam("token") String token) {
        // Long userId = SecurityUtils.getSecurityUserId();
        UserResultVo userResultVo = null;
        if (StringUtils.isNotEmpty(token)) {

            String[] s = token.split("_");
            String userId = s[1];
            log.info("【userId： {}】", userId);
            userResultVo = userService.findUserInfo(Long.valueOf(userId));
//            userResultVo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
//            userResultVo.setIntroduction("我是超级管理员");
//            //userResultVo.setName(SecurityUtils.getSecurityUserName());
            userResultVo.setAvatar(userResultVo.getHeadImage());
            userResultVo.setName(userResultVo.getRealname());
            log.info("【userResultVo ： {}】", userResultVo);
        }
        return ResultVoUtil.success(userResultVo);
    }

    @GetMapping("/listByCondition")
    public ResultVo listByCondition(UserReqVo userReqVo, PageVo pageVo) {
        pageVo.setPageNumber(pageVo.getPageNumber() - 1);
        Page<UserResultVo> page = userService.listByCondition(userReqVo, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }


    @PostMapping("/submitUser")
    public ResultVo submitUser(@RequestBody UserReqVo userReqVo) {
        log.info("【userReqVo ： {}】  ", userReqVo);

        //
        userService.submitUser(userReqVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/deleteUserById")
    public ResultVo deleteUserById(@PathParam("id") Long id) {

        userRepository.deleteById(id);

        return ResultVoUtil.success();
    }


}